package com.itheima.health.task;

import com.google.common.collect.Lists;
import com.itheima.health.common.Globalconstant;
import com.itheima.health.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;

//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QiniuUtils qiniuUtils;
    //3.添加定时任务
    //每日10点执行一次
    //@Scheduled(cron = "0 0 10 * * ?")
    //"0 15 10 * * ? 2005" 2225年的每天上午10:15触发
    //@Scheduled(cron = "0 15 10 * * ? 2225")
    private void configureTasks() {
        HashOperations operations = redisTemplate.opsForHash();
        Set keys = operations.keys(Globalconstant.WEB_IMG_REDIS_KEY);
        System.out.println("keys = " + keys);
        redisTemplate.delete(Globalconstant.WEB_IMG_REDIS_KEY);
        //分批删除
        Lists.partition(new ArrayList<String>(keys), 10).stream().forEach((listkeys)->{
            listkeys.stream().filter(key->key != null).forEach(key->{
                qiniuUtils.deleteFileFromQiniu(key);
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        });
    }
}